<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 华南支付
 */
class HuananController extends PayController{
    protected $config;

    public function pay($array){
        //$this->config = M("Channel")->where(array('id'=>$array['channel']))->find();
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'016']);
        }

        //$product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        //echo '<pre/>'; print_r($product);exit;
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'016']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['partner'] = $this->config['mch_id'];
        $data['sdorderno'] = $return['orderid'];
        $data['paymoney'] = number_format($return['amount'],2,'.','');
        $data['bankcode'] = '';
        switch($product['paytype']){
            //支付宝扫码
            case 3:
                $data['paytype'] = 'alipay';
                break;
            //微信扫码
            case 1:
                $data['paytype'] = 'weixin';
                break;
                //微信H5
            case 2:
                $data['paytype'] = 'wxh5';
                break;
            //支付宝H5
            case 4:
                $data['paytype'] = 'alipaywap';
                break;
            //qq
            case 8:
                $data['paytype'] = '87030';
                break;
            //qqH5
            case 10:
                $data['paytype'] = '87029';
                break;
            //银联快捷
            case 13:
                $data['paytype'] = 'quickbank';
                break;
                //
                break;
            //网银直连
            case 6:
                switch($product['code']){
                    //北京银行
                    case 'BOB':
                        $data['paytype'] = 'BCCB';
                        break;
                    //东亚银行
                    case 'BEA':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'016']);
                        break;
                    //中国工商银行
                    case 'ICBC':
                        $data['paytype'] = 'ICBC';
                        break;
                    //光大银行
                    case 'CEB':
                        $data['pnc'] = 'CEB';
                        break;
                    //广发银行
                    case 'CGB':
                        $data['paytype'] = 'GDB';
                        break;
                    //华夏银行
                    case 'HXB':
                        $data['paytype'] = 'HXB';
                        break;
                    //中国建设银行
                    case 'CCB':
                        $data['paytype'] = 'CCB';
                        break;
                    //交通银行
                    case 'BOCOM':
                        $data['paytype'] = 'BOCOM';
                        break;
                    //中国民生银行
                    case 'CMBC':
                        $data['paytype'] = 'CMBC';
                        break;
                    //南京银行
                    case 'NJCB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'016']);
                        break;
                    //宁波银行
                    case 'NBCB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'016']);
                        break;
                    //中国农业银行
                    case 'ABC':
                        $data['paytype'] = 'ABC';
                        break;
                    //平安银行
                    case 'PAB':
                        $data['paytype'] = 'PAB';
                        break;
                    //上海银行
                    case 'SHB':
                        $data['paytype'] = 'BOS';
                        break;
                    //上海浦东发展银行
                    case 'SPDB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'016']);
                        break;
                    //深圳发展银行
                    case 'SDB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'016']);
                        break;
                    //兴业银行
                    case 'CIB':
                        $data['paytype'] = 'CIB';
                        break;
                    //中国邮政储蓄银行
                    case 'PSBS':
                        $data['paytype'] = 'PSBC';
                        break;
                    //招商银行
                    case 'CMB':
                        $data['paytype'] = 'CMB';
                        break;
                    //浙商银行
                    case 'CZB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'016']);
                        break;
                    //中国银行
                    case 'BOC':
                        $data['paytype'] = 'BOCSH';
                        break;
                    //中信银行
                    case 'CTTIC':
                        $data['paytype'] = 'CNCB';
                        break;
                    default:
                        $this->showmessage('暂不支持此银行卡!',['code'=>'016']);
                }
                break;
            default:
                $this->showmessage('暂不支持此支付方式!',['code'=>'016']);
        }
        $data['notifyurl'] = $return['notifyurl'];
        $data['returnurl'] = $return['callbackurl'];

        $tmp = array();
        $tmp['partner'] = $data['partner'];
        $tmp['paymoney'] = $data['paymoney'];
        $tmp['sdorderno'] = $data['sdorderno'];
        $tmp['notifyurl'] = $data['notifyurl'];
        $tmp['returnurl'] = $data['returnurl'];
        ksort($tmp);

        $str = "";
        foreach($tmp as $k=>$v){
            $str .= $k . '=' . $v . '&';
        }

        $str .= $this->config['signkey'];
        $data['sign'] = md5($str);

        po($this->config['gateway'], $data);
    }

    //通知
    public function notifyurl(){
       //file_put_contents("a.txt", var_export($_REQUEST,true)."\r\n\r\n");
       //file_put_contents("a.txt", var_export($_POST,true)."\r\n\r\n", FILE_APPEND);
       //file_put_contents("a.txt", var_export($_GET,true)."\r\n\r\n", FILE_APPEND);
       //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
       //file_put_contents("a.txt", "华南： ".date("Y-m-d H:i:s",time())."\r\n\r\n",FILE_APPEND);

        $p = I("post.");

        if(!$p || empty($p)){
            exit("");
        }

        $sign = $p['sign'];
        unset($p['remark']);
        unset($p['sign']);

        //获取订单信息
        $orderid = $p['sdorderno'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> json_encode($p),
            "pay_orderid_rd"=> $p['sdpayno']
        ));
        Order::getInstance()->edit($find['id'],array("server_return"=>json_encode($p), "pay_orderid_rd"=>$p['sdpayno']));
        Ordermember::getInstance(getIdByBianHao($find['pay_memberid']))->edit($find['id'],array("server_return"=>json_encode($p), "pay_orderid_rd"=>$p['sdpayno']));
        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Huanan','title'=>$find['pay_yzh_tongdao']))->find();
        if(!$this->config){
            exit("channel_not_found");
        }

        if($p['status'] != 1){
            exit("pail_fail");
        }

        ksort($p);
        $str = "";
        foreach($p as $k=>$v){
            $str .= $k . '=' . $v . '&';
        }
        $str .= $this->config['signkey'];
        $sign2 = md5($str);

        if($sign != $sign2){
            exit("sign_worng");
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Huanan",0);

        //回调成功
        echo 'success'; exit;
    }

    public function callBackUrl(){
        echo '操作成功'; exit;
    }
}